package com.idnx.mynewsdemo.view;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.idnx.mynewsdemo.R;
import com.idnx.mynewsdemo.adapter.MainAdapter;
import com.idnx.mynewsdemo.database.DataBaseBean;
import com.idnx.mynewsdemo.entity.NewsBean;

import java.util.ArrayList;
import java.util.List;

public class HistoryActivity extends AppCompatActivity {

    private TextView back;
    private String userName;
    private MainAdapter mainAdapter;
    private DataBaseBean dataBaseBean;
    private RecyclerView recyclerView;
    private List<NewsBean.ResultBean.DataBean> dataBeans;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history);
        back = findViewById(R.id.back);
        recyclerView = findViewById(R.id.two_Rv);
        dataBaseBean = new DataBaseBean(this);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        Intent intent = getIntent();
        userName = intent.getStringExtra("username");
        dataBeans = new ArrayList<>();
        mainAdapter = new MainAdapter(dataBeans);
        recyclerView.setAdapter(mainAdapter);
        dataBeans = dataBaseBean.getUserClickRecords(userName);
        if (dataBeans.size() >0){
            mainAdapter.setNewInstance(dataBeans);
        }
        mainAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                NewsBean.ResultBean.DataBean bean = mainAdapter.getItem(position);
                Intent intent1 = new Intent(HistoryActivity.this, DetailActivity.class);
                intent1.putExtra("url", bean.getUrl());
                startActivity(intent1);
            }
        });
    }
}